﻿using System;

namespace Innovative.ProXr
{
	public enum RelayTimerStyle
	{
		Duration,
		Pulse
	}

	public class RelayTimer
	{
		private int _timerId = 0;

		public RelayTimerStyle TimerStyle { get; set; }
		public bool Automatic { get; set; }

		public int TimerId
		{
			get
			{
				return _timerId;
			}
			set
			{
				_timerId = value;

				if (_timerId < 0 || _timerId > 15)
				{
					throw new ArgumentOutOfRangeException();
				}
			}
		}
	}

	/// <summary>
	/// 
	/// </summary>
	public class TimerParameterConverter : IParameterConverter<RelayTimer>
	{
		RelayByte[] IParameterConverter<RelayTimer>.Convert(RelayTimer parameter)
		{
			RelayByte[] returnValue = new RelayByte[1];

			if (parameter.TimerStyle == RelayTimerStyle.Duration)
			{
				if (parameter.Automatic)
				{
					// ***
					// *** 50 to 65
					// ***
					returnValue[0] = (byte)(50 + parameter.TimerId - 1);
				}
				else
				{
					// ***
					// *** 90 to 105
					// ***
					returnValue[0] = (byte)(90 + parameter.TimerId - 1);
				}
			}
			else
			{
				if (parameter.Automatic)
				{
					// ***
					// *** 70 to 85
					// ***
					returnValue[0] = (byte)(70 + parameter.TimerId - 1);
				}
				else
				{
					// ***
					// *** 110 to 125
					// ***
					returnValue[0] = (byte)(110 + parameter.TimerId - 1);
				}
			}

			return returnValue;
		}
	}
}
